﻿using System;
using System.Linq;
using System.Text;

namespace MDD.PML.Utility
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                ArgsParser argsParser = new ArgsParser(args);

                Logger.Log(" ########################## ");
                StringBuilder sb = new StringBuilder();
                sb.Append("Start operation:");
                foreach (string arg in args)
                {
                    sb.Append(" ");
                    sb.Append(arg);
                }

                Logger.Log(sb.ToString());
                if (args.Count() == 0 || argsParser.Exists("help"))
                {
                    Console.WriteLine("Display Help text");
                    GetCommandUsage();
                    return;
                }

                if (!argsParser.Exists("o"))
                {
                    Console.WriteLine("Missing Operation");
                    GetCommandUsage();
                    return;
                }

                string strOperation = argsParser["o"];
                Operations operation = (Operations)Enum.Parse(typeof(Operations), strOperation);
                if (!Enum.IsDefined(typeof(Operations), operation))
                {
                    Console.WriteLine("Operation {0} is not defined", strOperation);
                    GetCommandUsage();
                    return;
                }

                OperationLibrary opsLibrary = new OperationLibrary(argsParser);
                ArgsValidator argsVal = new ArgsValidator();

                switch (operation)
                {
                    case Operations.PIM2PSM:
                        if (!argsVal.ValidatePIM2PSM(argsParser))
                        {
                            Logger.Log("Parameter validation failed");
                            return;
                        }

                        opsLibrary.PIM2PSM();
                        break;

                    case Operations.PSM2T:
                        if (!argsVal.ValidatePSM2Umbraco(argsParser))
                        {
                            Logger.Log("Parameter validation failed");
                            return;
                        }

                        opsLibrary.PSM2Umbraco();
                        break;

                    case Operations.T2PSM:
                        if (!argsVal.ValidateUmbraco2PSM(argsParser))
                        {
                            Logger.Log("Parameter validation failed");
                            return;
                        }

                        opsLibrary.Umbraco2PSM();
                        break;

                    case Operations.PSM2PIM:
                        if (!argsVal.ValidatePSM2PIM(argsParser))
                        {
                            Logger.Log("Parameter validation failed");
                            return;
                        }

                        opsLibrary.PSM2PIM();
                        break;

                    default:
                        break;
                }
                Logger.Log("Operation completed successfully");
            }
            catch (Exception ex)
            {
                Logger.Log(string.Format("ERROR: Exception {0}", ex.ToString()));
                return;
            }
        }

        private static void GetCommandUsage()
        {
            Console.WriteLine("Command usage:");
            Console.WriteLine("MDD.PML.Utility.exe -o PIM2PSM -filename xxx.cpml");
            Console.WriteLine("MDD.PML.Utility.exe -o PSM2Umbraco -filename xxx.upml");
            Console.WriteLine("MDD.PML.Utility.exe -o Umbraco2PSM -filename xxx.xml");
            Console.WriteLine("MDD.PML.Utility.exe -o PSM2PIM -filename xxx.upml");
        }       
    }
}

